/*
 * comlan
 */

package com.promfy.ui.listview.demo;


import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.promfy.R;
import com.promfy.db.demo.DBService;
import com.promfy.db.demo.Reminders;

public class MyListView extends LinearLayout implements OnClickListener{

	
	private Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			int what = msg.what;
			switch (what) {
			
			default:
				break;
			}
			super.handleMessage(msg);
		}
		
	};
	
	private LayoutInflater inflater;

	private Context context;

	private View view;

	private ListView listView;

	private List<Reminders> scores = new ArrayList<Reminders>();

	private BaseAdapter adapter;
	

	
	public MyListView(Context context) {
		super(context);
		this.context = context;
	
		initView();
		initData();
	}

	
	
	public MyListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}



	private void initView(){
		inflater = LayoutInflater.from(context);
		view = inflater.inflate(R.layout.mylistview, this);
		listView = (ListView)view.findViewById(R.id.listview_game_results);
		adapter = new MyListViewAdapter(context, 0, R.layout.mylistview_listview, scores);
		listView.setAdapter(adapter);
	}
	
	private void initData() {
		scores.add(new Reminders("20120222", "333"));
		scores.add(new Reminders("20120222", "324"));
		scores.add(new Reminders("20120222", "64"));
		scores.add(new Reminders("20120222", "36733"));
		scores.add(new Reminders("20120222", "86"));
		scores.add(new Reminders("20120222", "8"));
		adapter.notifyDataSetChanged();
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		//restart
		case 1:
		
			break;
		//back to mode
		case 2:

			break;
		//exit
		case 3:
			
			break;
		default:
			break;
		}
	}

	
	private class MyListViewAdapter extends ArrayAdapter {

		private LayoutInflater inflater;
		private ArrayList<Reminders> scoreList;

		private Bitmap collapseIcon;
		private Bitmap expandIcon;

		public MyListViewAdapter(Context context, int resource, int textViewResourceId, List objects) {
			super(context, resource, textViewResourceId, objects);
			inflater = LayoutInflater.from(context);
			scoreList = (ArrayList<Reminders>) objects;

		}

		@Override
		public Object getItem(int position) {
			return scoreList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.mylistview_listview, null);

				holder = new ViewHolder();
				holder.date = (TextView) convertView.findViewById(R.id.date_game_results_listview);
				holder.score = (TextView) convertView.findViewById(R.id.score_game_results_listview);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			try {

				Reminders score = (Reminders) getItem(position);
				holder.date.setText(score.getDate());
				holder.score.setText(score.getScore());
				
			} catch (Exception e) {
				e.printStackTrace();
			}

			return convertView;
		}

		class ViewHolder {
			TextView date;
			TextView score;
		}

		@Override
		public int getCount() {
			return scoreList.size();
		}

	}


}
